<?php

declare(strict_types=1);

namespace app\controller;

use app\model\User as UserModel;

class User
{
    protected $middleware = [\app\admin\middleware\Auth::class => ['except' => ['login']]];

    public function login()
    {
        $param = request()->param(['username', 'password']);

        $user = UserModel::where([
            'username' => $param['username'],
            'password' => md5($param['password'])
        ])->find();

        if (!$user) {
            return json(['code' => 40404, 'msg' => '用户不存在']);
        }

        return json([
            'code' => 20000,
            'msg' => '登录成功',
            'data' => [
                'token' => $user->token
            ]
        ]);
    }

    public function info()
    {
        if (empty(request()->user)) {
            return json(['code' => 40404, 'msg' => '用户不存在']);
        }

        return json(['code' => 20000, 'data' => request()->user]);
    }
}
